/**   
* @Title: CustomerService.java 
* @Package com.csis.system.service 
* @Description: TODO
* @author dailey  
* @date 2012-10-7
* @version V1.0   
*/
package com.csis.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csis.framework.db.BaseDao;
import com.csis.framework.domain.Condition;
import com.csis.system.common.IStaticData;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.Customer;
import com.csis.system.domain.CustomerFormBean;

/**
 * @author dailey
 *
 */
@Service
public class CustomerService {

	@Resource
	BaseDao baseDao;

	public Customer load(Integer id) {
		return baseDao.load(Customer.class, id);
	}

	public CustomerFormBean list(CustomerFormBean formBean) {
		String sql = getSearchSql(formBean);

		Condition condition = formBean.getCondition();
		if (condition != null) {
			sql = condition.getCondition(sql);
		}
		// importance
		int maxCount = getMaxCount(formBean);
		formBean.getPage().setMaxCount(maxCount);
		sql = formBean.getPage().perPageSql(sql);
		List<Customer> beans = baseDao.query4SingleTable(Customer.class, sql);
		formBean.setBeans(beans);
		return formBean;
	}

	/**
	 * @return
	 */
	public int getMaxCount(CustomerFormBean formBean) {
		String sql = getSearchSql(formBean);
		return baseDao.getMaxCount(sql);
	}

	/**
	 * @param formBean
	 * @return
	 */
	public String getSearchSql(CustomerFormBean formBean) {
		String sql = "SELECT * FROM " + IStaticData.TABLE_CUSTOMER + " WHERE 1=1 ";
		return sql;
	}

	@Transactional
	public void save(Customer bean) {
		if (bean.getId() == null) {
			baseDao.persist(bean);
		} else {
			baseDao.merge(bean);
		}
	}

	@Transactional
	public void remove(Integer id) {
		baseDao.remove(Customer.class, id);
	}

	@Transactional
	public void remove(ConvertArray<Integer> ids) {
		if (ids == null)
			return;
		for (Integer id : ids) {
			remove(id);
		}
	}
}
